package com.htz.agency.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htz.agency.mapper.AnnouncementMapper;
import com.htz.agency.pojo.Announcement;
import com.htz.agency.pojo.AnnouncementExpired;
import com.htz.agency.pojo.dto.AnnouncementDTO;
import com.htz.agency.pojo.query.AnnouncementQuery;
import com.htz.agency.pojo.vo.AnnouncementVO;
import com.htz.agency.service.AnnouncementService;
import com.htz.agency.util.PageResult;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tang
 * @date 2025年06月27日 20:27
 */
@Slf4j
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {
    @Autowired
    private AnnouncementMapper announcementMapper;
    @Override
    public PageResult<List<AnnouncementVO>> list(AnnouncementQuery query) {
        IPage<AnnouncementVO> page = new Page<>(query.getPage(), query.getLimit());
        log.info("查询参数{}", query.getTitle());
        announcementMapper.list(page, query);
        return new PageResult(0, "", (int)page.getTotal(), page.getRecords());
    }

    @Override
    public Boolean exDeletedByIds(List<Integer> list) {
        announcementMapper.exDeletedByIds(list);
        return true;
    }

    @Override
    public boolean deleteAll() {
        List<Integer> byDeletedIds = announcementMapper.getByDeleted();
        log.info("查询已删除的公告id{}", byDeletedIds);
        if(byDeletedIds.isEmpty()){
            return true;
        }
        Boolean flag = announcementMapper.deleteAll(byDeletedIds);
        if(flag){
            return true;
        }
        return false;
    }

    /**
     * 处理已过期的公告，将其归档。
     * 
     * @return 如果操作成功返回true，否则返回false
     */
    @Transactional
    public boolean expiredAnnouncement() {
        Date date = new Date();
        List<Announcement> byExpiredAnnouncements = announcementMapper.getByExpired(date);
        log.info("查询已过期的公告{}",byExpiredAnnouncements);
        // 查询没有过期的公告列表为空
        if(byExpiredAnnouncements.isEmpty()){
            return true;
        }

        List<AnnouncementExpired> announcementExpireds = new ArrayList<>();
        AnnouncementExpired announcementExpired = new AnnouncementExpired();
        // 将每个未过期的公告转换成已过期公告对象，并设置归档时间
        for (Announcement announcement : byExpiredAnnouncements){
            BeanUtils.copyProperties(announcement, announcementExpired);
            announcementExpired.setArchiveTime(date);
            announcementExpireds.add(announcementExpired);
            //删除已过期的
            announcementMapper.deleteById(announcement.getId());
        }
        Boolean flag = announcementMapper.saveExpiredAnnouncement(announcementExpireds);
        // 保存已过期的公告信息是否成功
        if(flag){
            return true;
        }
        return false;
    }



 /*   @Override
    public AnnouncementVO getById(Long id) {
        System.out.println("11111");
        log.info("查询id为{}的分类Mapper", id);
        AnnouncementVO byId = announcementMapper.getById(id);
        return byId;
    }*/
}
